﻿@{
    ViewBag.Title = "字段管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">模块结构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;"><i class="fa fa-minus"></i></a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">
      
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">字段列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default"><i class="fa fa-search"></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm"><i class="fa fa-refresh">刷新</i></a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
@Html.Partial("_PartialSystemPrivilegeDetail")
<script>
    $(function () {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initTreeData();
        initGird();
    });

    var zNodes,
              treeObj,
              setting,
              $grid,
              height;

    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            loadonce: false,
            datatype: "json",
            url: "/System/Field/GetFieldByMenuId",
            colModel: [
                { name: "FieldId", hidden: true },
                { label: "显示名称", name: "Label", width: 100, fixed: true },
                { label: "字段名称", name: "Name", index: "field.Name", width: 100, fixed: true },
                { label: "排序名称", name: "Index", index: "[Index]", width: 100, fixed: true },
                { label: "归属", name: "MenuName", index: "menu.Name", width: 100, fixed: true },
                { label: "列宽", name: "Width", width: 100, fixed: true },
                { label: "对齐方式", name: "Align", width: 80, fixed: true },
                { label: "格式化", name: "Formatter", width: 80 },
                { label: "排序方式", name: "Sorttype", width: 80 },
                { label: "排序", name: "OrderNo", align: "center", index: "field.OrderNo", width: 50, fixed: true },
                { label: "冻结", name: "IsFreeze", index: "field.IsFreeze", formatter: "YesOrNo", width: 50, align: "center", fixed: true },
                { label: "显示", name: "Hidden", width: 50, fixed: true, formatter: "YesOrNo", align: "center" },
                //{ label: "导出/打印", formatter: "YesOrNo", name: "CanbeDerive", width: 60, fixed: true },
                { label: "宽度固定", formatter: "YesOrNo", name: "Fixed", width: 60, fixed: true, align: "center" },
                { label: "备注", name: "Remark", index: "field.Remark", width: 100, fixed: true, align: "center" }
            ],
            height: height - 100,
            sortname: "menu.Name,field.OrderNo",
            sortorder: "asc",
            postData: { IsShowHidden: true }, 
            subGrid: true,
            subGridRowExpanded: function (subgridId, rowId) {
                var rowDatas = $grid.jqGrid('getRowData', rowId);
                if (typeof (rowDatas["FieldId"]) != "undefined") {
                    getById(Language.privilegeAccess.field, rowDatas["FieldId"]);
                    //获取html值
                    $("#" + subgridId).html($("#permissionUser").html());
                }
            }
        });
    }

    //获取权限
    function getById(access, id) {
        if (id != null && id != "") {
            UtilAjaxPost("/Common/Global/GetSystemPrivilegeDetailOutputsByAccessAndValue", { id: id, access: access },
                function (val) {
                    //角色
                    var roleHtml = template('role-template', val);
                    $("#role").html(roleHtml);

                    //岗位
                    var postHtml = template('post-template', val);
                    $("#post").html(postHtml);

                    //组
                    var groupHtml = template('group-template', val);
                    $("#group").html(groupHtml);

                    //组织机构
                    var organizationHtml = template('organization-template', val);
                    $("#organization").html(organizationHtml);

                    //人员
                    var userHtml = template('user-template', val);
                    $("#user").html(userHtml);
                }
            );
        } else {

            var val = [];
            //角色
            var roleHtml = template('role-template', val);
            $("#role").html(roleHtml);

            //岗位
            var postHtml = template('post-template', val);
            $("#post").html(postHtml);

            //组
            var groupHtml = template('group-template', val);
            $("#group").html(groupHtml);

            //组织机构
            var organizationHtml = template('organization-template', val);
            $("#organization").html(organizationHtml);

            //人员
            var userHtml = template('user-template', val);
            $("#user").html(userHtml);
        }
    }

    //初始化菜单
    function initTree() {
        //配置
        setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },

            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: onClickTree
            }
        };
        treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    }

    //初始化树结构
    function initTreeData() {
        UtilAjaxPostAsync("/System/Menu/GetHaveFieldPermissionMenu", null, function (data) {
            zNodes = data;
            initTree();
        });
    }

    //树点击触发
    function onClickTree(e, treeId, treeNode) {
        //赋值
        resetValue(treeNode.id);
        //加载数据
        getGridData();
    }

    //获取表格数据
    function getGridData() {
        //重新查询
        search();
    }

    //重新加载
    function reloadTreeAndGrid() {
        resetOrgId(null);
        initTreeData();
        getGridData();
    }

    //操作:新增
    function add() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var addUrl = "/System/Field/Edit";
        if (treeNode.length !== 0) {
            addUrl += "?menuId=" + treeNode[0].id + "&menuName=" + treeNode[0].name;
        }
        ArtDialogOpen(addUrl, "新增字段", 580);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function () {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Field/Edit?id=" + info.FieldId, "修改字段-" + info.Label, 580);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function () {
            ArtDialogConfirm(Language.common.deletemsg, function () {
                UtilAjaxPostWait(
                    "/System/Field/DeleteField",
                    { id: GridGetSingSelectData($grid).FieldId },
                    perateStatus
                );
            });
        });
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            getGridData();
        }
    }

    //重置组织机构
    function resetValue(id) {
        $("#MenuId").val(id);
    }


</script>